<?php

/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/1/22
 * Time: 16:33
 */
require_once $_SERVER['DOCUMENT_ROOT'] . "/weixin/lib/mysql.class.php";

class _Db
{
    protected static $db;
    protected static $conf = [
//        'host' => '127.0.0.1',
        'host' => '192.168.0.119',
        'port' => 3306,
        'user' => 'root',
        'passwd' => '',
//        'passwd' => '',
        'dbname' => 'bdm255853683_db',
        'prefix' => 'jd_wx_'
    ];
    protected static $instance;

    public static function instance()
    {
        if (isset(self::$instance)) {
            return self::$instance;
        } else {
            return self::$instance = new MMysql(self::$conf);
        }
    }
}

//dataHandel::instance();
//保存举报数据
class dataHandel
{
    public static function addComplainData()
    {
        $data['open_id'] = htmlspecialchars($_POST['open_id'], ENT_QUOTES);
        $data['content'] = htmlspecialchars($_POST['content'], ENT_QUOTES);
        $data['type'] = htmlspecialchars($_POST['river_id'], ENT_QUOTES);
        $data['pic'] = htmlspecialchars($_POST['pic'], ENT_QUOTES);
        $data['create_time'] = time();
        $res = _Db::instance()->insert("complain", $data);
        if ($res) {
            ajaxSuccess('提交成功');
        } else {
            ajaxError('提交失败');
        }
    }

    public static function saveUploadImg()
    {
        if (!isset($_POST['base64img'])) return false;
        $base64_image_content = $_POST['base64img'];
        $output_directory = './upload'; //设置图片存放路径

        /* 检查并创建图片存放目录 */
        if (!file_exists($output_directory)) {
            mkdir($output_directory, 0777);
        }
        /* 根据base64编码获取图片类型 */
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
            $image_type = $result[2]; //data:image/jpeg;base64,
            $output_file = $output_directory . '/' . md5(time()) . '.' . $image_type;
        }
        /* 将base64编码转换为图片编码写入文件 */
        $image_binary = base64_decode(str_replace($result[1], '', $base64_image_content));
        if (file_put_contents($output_file, $image_binary)) {
//            echo $output_file; //写入成功输出图片路径
            echo json_encode(['path' => '/weixin' . trim($output_file, '.'), 'code' => 1]);
        } else {
            echo json_encode(['code' => -1]);
        }
    }

    public static function upScroll()
    {
        $request_time = isset($_GET['request_time']) ? htmlspecialchars($_GET['request_time'], ENT_QUOTES) : time();
        $open_id = isset($_GET['open_id']) ? htmlspecialchars($_GET['open_id'], ENT_QUOTES) : 'oBvWv1JFlv41phpbTI29LhW6Mpts';
        $list = _Db::instance()->_doQuery("SELECT id,FROM_UNIXTIME(create_time)AS create_time,SUBSTRING(content,1,10) AS title FROM jd_wx_complain WHERE open_id='$open_id' AND create_time>UNIX_TIMESTAMP(NOW())  ORDER by id ASC  LIMIT 0,20");
        echo json_encode(['list' => $list, 'page' => 1, 'request_time' => time()]);

    }

    public static function getList()
    {
        $open_id = isset($_GET['open_id']) ? htmlspecialchars($_GET['open_id'], ENT_QUOTES) : 'oBvWv1JFlv41phpbTI29LhW6Mpts';
        $list = _Db::instance()->_doQuery("SELECT id,FROM_UNIXTIME(create_time)AS create_time,SUBSTRING(content,1,10) AS title FROM jd_wx_complain WHERE open_id='$open_id'  ORDER by id DESC  LIMIT 0,20");
//        foreach ($list as $index => $item) {
//            $list[$index]['title']="<a href=".$_SERVER['HTTP_HOST'].">".$item['title']."</a>";
//        }
        echo json_encode(['list' => $list, 'page' => 1, 'request_time' => time()]);
    }

    public static function downScroll()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        $index = $page * 20;
        $open_id = isset($_GET['open_id']) ? htmlspecialchars($_GET['open_id'], ENT_QUOTES) : 'oBvWv1JFlv41phpbTI29LhW6Mpts';
        $list = _Db::instance()->_doQuery("SELECT id,FROM_UNIXTIME(create_time)AS create_time,SUBSTRING(content,1,10) AS title FROM jd_wx_complain WHERE open_id='$open_id' ORDER by id  DESC  LIMIT $index,20");
        echo json_encode(['list' => $list, 'page' => $page, 'request_time' => time()]);
    }

    public static function repliesHandel()
    {
        $data['wx_complain_id'] = htmlspecialchars($_POST['wx_complain_id'], ENT_QUOTES);
        $data['open_id'] = htmlspecialchars($_POST['open_id'], ENT_QUOTES);
        $data['content'] = htmlspecialchars($_POST['content'], ENT_QUOTES);
        $data['create_time'] = time();
        $res = _Db::instance()->insert("replies", $data);
        if ($res) {
            ajaxSuccess('提交成功');
        } else {
            ajaxError('提交失败');
        }
    }
}

$ref = new ReflectionClass('dataHandel');
$methodArr = $ref->getMethods();
foreach ($methodArr as $item) {
    $funNameArr[] = $item->getName();
}
if (isset($_GET['fun'])) {
    $reqFun = $_GET['fun'];
    if (in_array($reqFun, $funNameArr)) {
        try {
            dataHandel::$reqFun();
        } catch (ErrorException $e) {
            exit("$reqFun not exists:");
        }
    } else {
        exit("$reqFun not exists:");
    }
}

function ajaxError($msg = '', $status = -1, $fields = array())
{
    header('Content-Type:application/json; charset=utf-8');
    $data = array('status' => $status, 'msg' => $msg, 'fields' => $fields);
    echo json_encode($data);
    exit;
}

function ajaxSuccess($msg, $status = 1, $_data = array())
{
    header('Content-Type:application/json; charset=utf-8');
    $data = array('status' => $status, 'msg' => $msg, 'data' => $_data);
    echo json_encode($data);
    exit;
}
